home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #46 (Jul 89) / Terminal / SerI⁄O.asm next >
Assembly Source File  |  1989-03-09  |  7KB  |  220 lines

  1. ; File: SerI/O.asm
  2. ;----------------------------------------------------------------------
  3. ; SerI/O.asm consists of the following stand-alone serial I/O routines:
  4. ;
  5. ; Function    OpenSerial(Port: integer): OSErr
  6. ; Function    Config(Port, SerVal: integer): OSErr
  7. ; Function    GetSerial(Port: integer, buffer: pointer): CharCnt
  8. ; Function    PutSerial(Port: integer, CharBuf: pointer, count: longint): OSErr
  9. ; Function    SetBuf(Port: integer, BufSiz: long): OSErr
  10. ; Port refers to the port reference number, which is -6 for the modem 
  11. ; input port (port A) or -8 for printer input port  (port B)
  12. ;
  13. ; For a great description of how to write modular assembly language
  14. ; routines see Dan Weston's "The Complete Book of Macintosh Assembly
  15. ; Language Programming Volume I", Appendix D
  16. ;
  17. ;  1/14/88    Working version of the routines
  18. ;
  19. ; written by Frank Henriquez
  20. ;----------------------------------------------------------------------
  21. ; Register usage:
  22. ; d0: general purpose. 
  23. ; d1, d2, d3, d4, d5, d6, d7 : not used.
  24. ;
  25. ; a0: general purpose, used for buffers and parameter blocks.
  26. ; a1: general purpose.
  27. ; a2, a3, a4: not used.
  28. ; a6: used as the local stack frame pointer
  29. ; a5, a7 : system use. 
  30. ;----------------------------------------------------------------------
  31.  
  32. Include        Traps.D
  33. Include        SysEqu.D
  34.  
  35.     XDEF    OpenSerial
  36.     XDEF    Config
  37.     XDEF    GetSerial
  38.     XDEF    PutSerial
  39.     XDEF    SetBuf 
  40.  
  41. ; equates for each stack frame
  42.  
  43. pBlock        equ    -ioQelSize
  44.  
  45. OpenPar        equ     2        ; OpenSerial - # of parameter words on stack
  46. OpenRes        equ    10        ; OpenSerial - offset to result word
  47. OpenPort    equ     8        ; OpenSerial - offset to Port Reference #
  48.  
  49. ConfPar        equ     4        ; Config - # of parameter words on stack
  50. ConfRes        equ    12        ; Config - offset to result word
  51. ConfPort    equ    10        ; Config - offset to port Reference #
  52. ConfVal        equ     8        ; Config - offset to port config value
  53.  
  54. GetPar        equ     6        ; you've probably figured the pattern
  55. GetCnt        equ    14        ; offset to character count
  56. GetPort        equ    12        ; out by now...
  57. GetBuf        equ     8         ; offset to character buffer pointer
  58.  
  59. PutPar        equ    10
  60. PutPort        equ    16
  61. PutBuf        equ    12        ; offset to character buffer pointer
  62. PutCnt        equ     8        ; offset to character count pointer
  63. PutRes        equ    18        ; dummy result 
  64.  
  65. SetPar        equ     6
  66. SetRes        equ    14        ; offset to SetBuf result word
  67. SetPort        equ    12
  68. SetSiz        equ     8        ; offset to new buffer size
  69.  
  70. ; misc. equates
  71.  
  72. SerReset    equ     8        ; from Inside Macintosh
  73. SerSetBuf    equ     9        ; from Inside Macintosh
  74. ioNamePtr    equ    18        ; missing from MDS
  75.  
  76.  
  77. ;-------------- OpenSerial ------------------
  78. ; Opens the modem port for reads and writes.
  79. ;
  80. ; Procedure    OpenSerial(Port: integer): OSErr
  81.  
  82. OpenSerial
  83.     link    a6,#pBlock        ; local space for parameter block
  84.     lea    pBlock(a6),a0        ; point to parameter block
  85.     lea    '.AIn',a1        ; assume port A input
  86.     cmpi.w    #-6,OpenPort(a6)    ; open port B if not -6 (port A)
  87.     beq.s    @1
  88.     lea    '.Bin',a1        ; open port B input
  89. @1    bsr.s    Openit
  90.     lea    '.AOut',a1        ; assume port A output
  91.     cmpi.w    #-6,OpenPort(a6)
  92.     beq.s    @2
  93.     lea    '.Bout',a1        ; open port B output
  94. @2    bsr.s    Openit
  95.  
  96. OExit    move.w    d0,OpenRes(a6)        ; procedure returns a 0 if no errors
  97.     _KillIO                ; kill any pending calls
  98.     unlk    a6            ; discard local stack area
  99.     move.l    (sp)+,a0        ; get return address
  100.     addq.l    #OpenPar,sp        ; clean up stack
  101.     jmp    (a0)            ; end of InitSerial
  102.  
  103. Openit    
  104.     move.l    a1,ioNamePtr(a0)    ; tell the Device Manager which
  105.     _Open                ; port to open
  106.     rts
  107.  
  108.  
  109. ;----------- Configure ports --------------
  110. ; Sets the Port to the baud rate, data length,
  111. ; parity, stops, etc. held in SerVal.
  112. ;
  113. ; Procedure    Config(Port, SerVal : integer) : OSErr
  114.  
  115. Config
  116.     link    a6,#pBlock        ; local space for parameter block
  117.     lea    pBlock(a6),a0        ; point to parameter block
  118.     move.w    ConfPort(a6),d0        ; get Input Reference number
  119.     bsr.s    doConf            ; configure the input side
  120.     tst.w    d0            ; check for errors and
  121.     bne.s    CExit            ; exit with the error flag in d0
  122.     move.w    ConfPort(a6),d0        ; get Input Ref number
  123.     subq.w    #1,d0            ; and make it the output ref num
  124.     bsr.s    doConf            ; configure the output side
  125.  
  126.  
  127. CExit    move.w    d0,ConfRes(a6)        ; save result
  128.     unlk    a6
  129.     move.l    (sp)+,a0
  130.     addq.l    #ConfPar,sp        ; and clean up the stack
  131.     jmp    (a0)            ; end of Configure
  132.  
  133. doConf    move.w    d0,ioRefNum(a0)        ; port #
  134.     move.w    #SerReset,csCode(a0)    ; reset port to
  135.     move.w    ConfVal(a6),csParam(a0)    ; these new settings
  136.     _Control
  137.     rts
  138.  
  139.  
  140. ;---------------- SetBuf -------------------
  141. ; increases the size of the Port input buffer
  142. ;
  143. ; SetBuf(Port:Integer, BufSiz:long):OSErr
  144.  
  145. SetBuf    link    a6,#pBlock        ; local space for parameter block
  146.     move.l    SetSiz(a6),d0
  147.     beq.s    @1            ; if size = 0, reset input buffer
  148.     _NewPtr                ; get a nonrelocatable block
  149.     tst.l    d0            ; abort if couldn't get it
  150.     bne    Exit
  151.     move.l    a0,a1            ; save block ptr in a1
  152.     move.l    SetSiz(a6),d0        ; reload the buffer size
  153. @1    lea    pBlock(a6),a0        ; point to parameter block
  154.     move.w    SetPort(a6),ioRefNum(a0)
  155.     move.w    #SerSetBuf,csCode(a0)
  156.     move.l    a1,csParam(a0)         ; pointer to new buffer
  157.     move.w    d0,csParam+4(a0)
  158.     _Control
  159. Exit    move.w    d0,SetRes(a6)
  160.     unlk    a6            ; standard exit routine
  161.     move.l    (sp)+,a0
  162.     addq.l    #SetPar,sp
  163.     jmp    (a0)            ; end of SetBuf
  164.  
  165.  
  166. ;--------------- GetSerial ----------------
  167. ; GetSerial checks the Port to see if any
  168. ; characters have been received, and proceeds
  169. ; to read them into the input buffer.
  170. ; If none have been received, GetSerial returns
  171. ; a 0 in the character count variable.
  172. ;
  173. ; Procedure    GetSerial(Port:integer, buffer:pointer) : CharCnt: long
  174.  
  175. GetSerial
  176.     link    a6,#pBlock
  177.     lea    pBlock(a6),a0
  178.     move.w    GetPort(a6),ioRefNum(a0)
  179.     move.w    #2,csCode(a0)        ; call SerGetBuf
  180.     _Status
  181.     move.l    csParam(a0),d0        ; get # of characters received
  182.     move.l    d0,GetCnt(a6)
  183.     beq.s    @2            ; if none, then leave, if there are
  184.                     ; characters available, read them.
  185. @1    move.w    GetPort(a6),ioRefNum(a0)
  186.     move.l    GetBuf(a6),ioBuffer(a0)    ; the input buffer
  187.     move.l    d0,ioReqCount(a0)    ; read the characters
  188.     _Read
  189. @2    unlk    a6            ; standard exit routine
  190.     move.l    (sp)+,a0
  191.     addq.l    #GetPar,sp        ; leave char count on stack
  192.     jmp    (a0)            ; end of GetSerial
  193.  
  194.  
  195. ;--------------- PutSerial ----------------
  196. ; PutSerial sends the characters in the buffer
  197. ; out the Port. This routine is pretty
  198. ; primitive - it ignores any pending writes
  199. ; and errors, but...it works. It returns a dummy result.
  200. ;
  201. ; function    PutSerial(Port:integer, CharBuf:pointer, count: pointer): OSErr
  202.  
  203. PutSerial
  204.     link    a6,#pBlock        ; local space for parameter block
  205.     lea    pBlock(a6),a0        ; point to parameter block
  206.     move.w    PutPort(a6),d0        ; get the port ref #
  207.     subq.w    #1,d0            ; make it the port output ref num
  208.     move.w    d0,ioRefNum(a0)        ; modem output port
  209.     move.l    PutBuf(a6),ioBuffer(a0)    ; point to buffer
  210.     move.l    PutCnt(a6),a1        ; get pointer to char count
  211.     move.l    (a1),ioReqCount(a0)    ; and put char count here
  212.     _Write
  213.     move.w    #0,PutRes(a6)        ; save a 0 as a dummy result
  214.     unlk    a6            ; standard exit routine
  215.     move.l    (sp)+,a0
  216.     adda.l    #PutPar,sp
  217.     jmp    (a0)            ; end of PutSerial
  218.  
  219.